package edu.neu.javaweb.controller;

import edu.neu.javaweb.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;


@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        log.info(req.getQueryString());
        String username = req.getParameter("username");
        String passwd = req.getParameter("passwd");
        String sex = req.getParameter("sex");
        String college = req.getParameter("college");

        log.info("username: " + username);
        log.info("password: " + passwd);
        log.info("sex: " + sex);
        log.info("college: " + college);

        if (new UserService().addUser(username, passwd, sex, college) == true){
            resp.sendRedirect("login.html?reg=true");
        }
        else {
            log.error("用户注册，主键重复");
            resp.sendRedirect("register.html?fail=true");
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req, resp);
    }
}
